package com.zxk.blog.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class TestAspect {

    @Pointcut("execution(* com.zxk.blog.controller.TestController.*(..))")
    public void commonMethod(){

    }

    @Before("commonMethod()")
    public void testBefore(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String queryString = request.getQueryString();
        request.getParameterMap().forEach((k,v) -> {
            System.out.println(k.toString()+"*********"+v[0].toString());
        });
    }


//    @Around("execution(* com.zxk.blog.controller.TestController.*(..))")
//    public void testAround(ProceedingJoinPoint proceedingJoinPoint){
//        ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        String queryString = request.getQueryString();
//        for (Object o : proceedingJoinPoint.getArgs()) {
//            System.out.println(o.toString());
//        }
//    }


}
